<template>
  <div class="Categorybox">
    <el-dropdown>
    <span class="el-dropdown-link" style="font-size: 20px;margin-left: 30px;margin-top: 5px">
      全部
      <el-icon class="el-icon--right">
        <arrow-down />
      </el-icon>
    </span>
      <template #dropdown>
        <el-dropdown-menu v-for="(item,index) in allCategory.categories" :key="item">
          <el-dropdown-item  style="background-color: white">
            <span style="color: #8e09dc">{{item}}:</span>
              <span v-for="item in List[index]" :key="item">
                 <div class="allCategoryList" >
                <span style="margin-left: 50px" @click="getplayListbyCategory(item.name)">{{item.name}}</span>
                 </div>
              </span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
    <div v-for="(item,index) in Category" :key="item" class="CategoryList">
       <span @click="getplayListbyCategory(item.name);clickItem(index)" :class="addbkColor(index)">{{item.name}}</span>
    </div>
  </div>
  <div class="box2">
    <div></div>
    <div class="playListBox">
      <div class="playListList" v-for="item in playList" :key="item">
        <img class="img" v-lazy="item.coverImgUrl" @click="getRankdetail(item.id)">
        <p style="letter-spacing: 2px;text-indent: 20px">{{ item.name }}</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import {reactive, ref} from "vue";
import axios from "axios";
import {useplayListStore,} from "@/Store/Pinia/pinia";
import { useRouter,useRoute } from "vue-router";
import {baseURL} from "@/request/index.js";
import {ArrowDown, ArrowRight} from "@element-plus/icons-vue";
const route=useRoute();
const name2=ref(route.query.name);
const router=useRouter();
const playListStore=useplayListStore();
const allCategory=ref([]);
const Category=ref([]);
const playList=ref([]);
const currentIndex=ref(-1);
const bkcolor=ref(true)
const List=reactive([[]]);

const gethotplaylistCategory= () => {  //获取热门歌单分类
  axios.post(baseURL+"/playlist/hot").then((res) => {
    console.log(res);
    Category.value=res.data.tags;
  })
}

const getallplaylistCategory= () => {  //获取全部歌单分类
  axios.post(baseURL+"/playlist/catlist").then((res) => {
    console.log(res);
    allCategory.value=res.data;
    for(let i=0;i<5;i++){
      List[i]=reactive([]);
      // console.log(List.value[i].value)
    }
    allCategory.value.sub.forEach((item)=>{
      List[item.category].push(item);
    })
    console.log(List)
  })
}

const gethotplayListbyCategory1=()=>{ //匹配歌单
  axios.post(baseURL+"/top/playlist?limit=100&order=hot&cat="+name2.value).then((res) => {
    console.log(res);
    playList.value=res.data.playlists;
  })
}
getallplaylistCategory();
gethotplaylistCategory();
gethotplayListbyCategory1();

function getplayListbyCategory(name){ //匹配歌单
  axios.post(baseURL+"/top/playlist?limit=100&order=hot&cat="+name).then((res) => {
    // console.log(res);
    playList.value=res.data.playlists;

  })
}

function getRankdetail(id){  //查看歌单详情
   playListStore.getcurrentplayList(id);
   router.push({path: '/homePage/songsList/1'});
}
function clickItem(index) {
  currentIndex.value = index;
}
function  addbkColor(index) {
  if (currentIndex.value == index) {
    return {active: bkcolor.value}
  }
}
</script>

<style scoped>
.Categorybox{
  width: 100%;
  height: 50px;
  display: flex;
}
.Categorybox .CategoryList{
  width: 100px;
  height: 40px;
  margin-right: 10px;
  /*background-color: #e9ba10;*/
  text-align: center;
  font-size: 20px;
  margin-left: 10px;
  cursor: pointer;

}
.box2{
  width: 100%;
  height: 400px;
  overflow-y:scroll;
  overflow-x:hidden;
}
.playListBox{
  display: flex;
  flex-wrap:wrap;
}

.playListBox .playListList{
  width: 200px;
  height: 250px;
  margin-left: 30px;
  margin-top: 20px;
  transition:All 0.4s ease-in-out;
}
.playListBox .playListList:hover{
  color: #8e09dc;
  transform:translate(0,-20px);
}
.playListBox .playListList .img{
  width: 200px;
  height: 200px;
}
.active{
  color: #42b983;
}
.allCategoryList{
width: 100%;
}
.allCategoryList span:hover{
  color: #42b983;
}


</style>